﻿using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace TCP.SocketHelper
{
    /// <summary>
    /// TCPSocket 连接的基类
    /// 
    /// <para>基于.Net Standard Library 2.0</para>
    /// <para>若要引用，要求.Net Framework 在4.6以上</para>
    /// </summary>
    public abstract class BaseTCPSocket
    {

        #region --Protected Field

        /// <summary>
        /// 是否已经连接
        /// </summary>
        protected bool _IsConnect = false;

        /// <summary>
        /// 连接目标的IP
        /// </summary>
        protected string _TargetIP;

        /// <summary>
        /// 连接目标的端口
        /// </summary>
        protected int _TargetPort;

        /// <summary>
        /// 与目标连接的Socket
        /// </summary>
        protected Socket _Socket;


        #endregion

        #region --Public Property

        /// <summary>
        /// 是否已经连接
        /// </summary>
        public bool IsConnect
        {
            get
            {
                return _IsConnect;
            }
        }

        /// <summary>
        /// 远程IP
        /// </summary>
        public string RemoteIP
        {
            get
            {
                if(_Socket == null)
                {
                    return "null";
                }

                return _Socket.RemoteEndPoint.ToString();
            }
        }

        #endregion

        #region --Public Callback Event

        /// <summary>
        /// 获取日志回调
        /// </summary>
        public Action<object, int> LogAction = null;

        /// <summary>
        /// 中断连接时的回调
        /// </summary>
        public Action<BaseTCPSocket> OnNetDropAction = null;

        /// <summary>
        /// 接收数据回调
        /// </summary>
        public Action<byte[]> ReceiveMsgAction = null;

        #endregion

        public virtual void StartConnect()
        {
        }

        /// <summary>
        /// 直接声明一个Socket连接
        /// <para>一般用于服务器上的客户端Socket</para>
        /// </summary>
        /// <param name="socket"></param>
        public virtual void SetSocketClient(Socket socket)
        {
            _Socket = socket;
            _TargetIP = socket.RemoteEndPoint.ToString();
            _TargetPort = 100;
            _IsConnect = true;
        }

        /// <summary>
        /// 输出日志
        /// </summary>
        /// <param name="log"></param>
        /// <param name="type">输出日志的类型
        /// <para>0-Log</para>
        /// <para>1-Error</para>
        /// <para>2-Warning</para>
        /// </param>
        public virtual void Log(object log, int type = 0)
        {
            LogAction?.Invoke(log, type);
        }

        /// <summary>
        /// 中断连接
        /// </summary>
        /// <param name="error"></param>
        protected virtual void NetDrop(object error)
        {
            _IsConnect = false;
            Log(string.Format(" {0} Network dropped, error: {1}", GetType(), error),1);
            if (OnNetDropAction != null)
            {
                OnNetDropAction(this);
            }
        }
    }
}
